package com.thoughtworks.preuniversity.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by padmavb on 6/12/14.
 */
//Job: Maintains the data about books , perform book  related operations
public class Library {

    private List<Book> catalogue = new ArrayList<Book>();
    private List<Book> checkOutBooks = new ArrayList<Book>();
    private List<Movie> movies=new ArrayList<Movie>();

    UserInterface userInterface = new UserInterface();

    public Library() {

        Book book1 = new Book("C", "Padma", 2013);
        catalogue.add(book1);

        Book book2 = new Book("Maths", "John", 2014);
        catalogue.add(book2);

        Book book3 = new Book("Data structures", "Jack", 2012);
        catalogue.add(book3);

        Movie movie1=new Movie("3 idiots",2000,"Srikumar",5);
        movies.add(movie1);

        Movie movie2=new Movie("Si",2000,"DilRaj",4);
        movies.add(movie2);
    }

    public void checkOut(String bookToCheckOut) {

        if (isAvailableInCorrespondingLists(bookToCheckOut, catalogue)) {

            Book book=getCorrespondingBookObj(bookToCheckOut,catalogue);

            checkOutBooks.add(book);
            catalogue.remove(book);

            userInterface.displayMessage("Thank you! enjoy the book");


        } else
            userInterface.displayMessage("That book is not available");

    }

    public boolean isAvailableInCorrespondingLists(String bookName,List<Book> listOfBooksToSearch ) {

        for (int index = 0; index < listOfBooksToSearch.size(); index++) {

            if (bookName.equals(listOfBooksToSearch.get(index).getTitle()))
                return true;

        }
        return false;
    }

    public void returnBook(String bookToReturn) {

        if (isAvailableInCorrespondingLists(bookToReturn, checkOutBooks)) {

            Book book=getCorrespondingBookObj(bookToReturn,checkOutBooks);

            catalogue.add(book);
            checkOutBooks.remove(book);

            userInterface.displayMessage("Thank you for returning the book");


        } else
            userInterface.displayMessage("That is not a valid book to return");

    }

    public Book getCorrespondingBookObj(String bookName,List<Book> listOfBooksToGiveObject){

        for(int index=0;index<listOfBooksToGiveObject.size();index++){

            if(bookName.equals(listOfBooksToGiveObject.get(index).getTitle()))
                return listOfBooksToGiveObject.get(index);
        }
        return null;
    }

    public List<Book> getCheckoutBooks() {

        return checkOutBooks;
    }

    public List<Book> getCatalogue() {

        return catalogue;
    }

    public List<Movie> getMovies() {
        return movies;
    }
}
